%% Author: dikla
%% Created: 07/05/2010
%% Description: TODO: Add description to new_file
-module(test).

%%
%% Include files
%%
-include("database.hrl").
%%
%% Exported Functions
%%

-export([f/0,init/0,info/0,run/0]).


f()->
	mnesia:create_schema([node()]),
	mnesia:start().
	


init() ->
    mnesia:create_table(company_stocks,
                        [{attributes, record_info(fields, company_stocks)}]),
	 mnesia:create_table(user_stock_table,
                        [{attributes, record_info(fields, user_stock_table)}]),

	mnesia:create_table(users,
                        [{attributes, record_info(fields, users)}]),
	mnesia:create_table(favorites,
                        [{attributes, record_info(fields, favorites)}]),
	mnesia:create_table(news,
                        [{attributes, record_info(fields, news)}]),
	mnesia:create_table(bid_queue,
                        [{attributes, record_info(fields, bid_queue)}]),
	mnesia:create_table(ask_queue,
                        [{attributes, record_info(fields, ask_queue)}]),
	mnesia:create_table(counters,
                        [{attributes, record_info(fields, counters)}]).

info()->
	mnesia:info().

%%
%% API Functions
%%




insert_to_DB(Record) ->
    
    Fun = fun() ->
                  mnesia:write(Record)
                                
          end,
    mnesia:transaction(Fun).
	
 example()->
	
%--------------------@@@@@@@@@@@@@@@@@@@@@@@@@@@_____USERS____  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@---------------------	
	User1=	#users{id=1,
				name=dikla,
				money=9870,
				value=0,
				profit=0},
	 insert_to_DB(User1),
	
	User2=	#users{id=2,  
				name=lilach,
				money=9700.5,
				value=0,
				profit=0},
	 insert_to_DB(User2),
	
	User3=	#users{id=3,
				name=ido,
				money=9833,
				value=0,
				profit=0},
	 insert_to_DB(User3),
%--------------------@@@@@@@@@@@@@@@@@@@@@@@@@@@_____FAVORITES____  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@---------------------	
  Favorite1=#favorites{id=1,
					    stock_id=3,
 			   			user_id=1},
  	insert_to_DB(Favorite1),

Favorite4=#favorites{id=4,
					  stock_id=2,
 			 			user_id=1},
  	insert_to_DB(Favorite4),

Favorite2=#favorites{id=2,
						 stock_id=3,
			   user_id=2},
	insert_to_DB(Favorite2),

Favorite3=#favorites{id=3,
						 stock_id=4,
			   user_id=3},
	insert_to_DB(Favorite3),


	%-----------------------@@@@@@@@@@@@@@@@@@@@@@@@____STOCK TABLES____ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@---------------------
		%-----------------------FOR USER ID 1---------------------
	Stock_Table0= #user_stock_table{id=0,
									stock_id=1,
									user_id=1,
									total=5,
									price=7
									},
	insert_to_DB(Stock_Table0),
	Stock_Table9= #user_stock_table{id=9,
									stock_id=1,
									user_id=1,
									total=7,
									price=8
									},
	insert_to_DB(Stock_Table9),
	
	Stock_Table1= #user_stock_table{id=1,
									stock_id=2,
									user_id=1,
									total=10,
									price=5
									},
	insert_to_DB(Stock_Table1),
	
	Stock_Table2= #user_stock_table{id=2,
									stock_id=3,
									user_id=1,
									total=3,
									price=8},
	insert_to_DB(Stock_Table2),
	
		%-----------------------FOR USER ID 2---------------------
	Stock_Table3= #user_stock_table{id=3,
									stock_id=2,
									user_id=2,
									total=15,
									price=9.5},
	insert_to_DB(Stock_Table3),
	Stock_Table4= #user_stock_table{id=4,
									stock_id=3,
									user_id=2,
									total=12,
									price=6},
	insert_to_DB(Stock_Table4),
	
	Stock_Table5= #user_stock_table{id=5,
									stock_id=4,
									user_id=2,
									total=17,
									price=5},
	insert_to_DB(Stock_Table5),

		%-----------------------FOR USER ID 3---------------------
	Stock_Table6= #user_stock_table{id=6,
									stock_id=1,
									user_id=3,
									total=5,
									price=5},
	insert_to_DB(Stock_Table6),
	
	Stock_Table7= #user_stock_table{id=7,
									stock_id=2,
									user_id=3,
									total=13,
									price=5},
	insert_to_DB(Stock_Table7),
	
	Stock_Table8= #user_stock_table{id=8,
									stock_id=4,
									user_id=3,
									total=11,
									price=7},
	insert_to_DB(Stock_Table8),
						
	
	
%--------------------@@@@@@@@@@@@@@@@@@@@@@@@@@@_____COMPANY____  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@---------------------	
	 Comp= #company_stocks{id=1,
						company_name= "gold inc",
						domain=gold,
						general= "the gold world",
						total_exist= 450,
						company_val = 5.6,
						dealsCountCounter=0,
						current_value=5.6,
						last_weight =0
						},
	 insert_to_DB(Comp),					   
	  Comp2= #company_stocks{domain=phone,
						company_name= "bezeq",
						id=2,
						general= "a phone company",
						total_exist= 150,
						company_val = 9,
						dealsCountCounter=0,
						current_value=9,
						last_weight =0},
	 insert_to_DB(Comp2),
	Comp3= #company_stocks{domain=hightech,
						company_name= "Intel",
						id=3,
						general= "Intel Corp",
						total_exist= 150,
						company_val = 7.4,
						dealsCountCounter=0,
						current_value=7.4,
						last_weight =0},
	 insert_to_DB(Comp3),
	
	Comp4= #company_stocks{domain=food,
						company_name= "Nestle",
						id=4,
						general= "food & coffe",
						total_exist= 150,
						company_val = 4.3,
						dealsCountCounter=0,
						current_value=4.3,
						last_weight =0},
	 insert_to_DB(Comp4),
	
	
%--------------------@@@@@@@@@@@@@@@@@@@@@@@@@@@_____BID____  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@---------------------	
Bid0=#bid_queue{bid_id=0,
					stock_id=1,
					user_id=1,
					amount=10,
					price=8
				},	
		insert_to_DB(Bid0),
	
Bid1=#bid_queue{bid_id=1,
					stock_id=4,
					user_id=2,
					amount=4,
					price=10
				},
	insert_to_DB(Bid1),
	
Bid2=#bid_queue{bid_id=2,
					stock_id=2,
					user_id=3,
					amount=8,
					price=10
				},
	insert_to_DB(Bid2),

%--------------------@@@@@@@@@@@@@@@@@@@@@@@@@@@_____ASKS____  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@---------------------	
Ask0=#ask_queue{ask_id=0,
					stock_id=4,
					user_id=1,
					amount=15,
					price=4
				},
	insert_to_DB(Ask0),
	
Ask1=#ask_queue{ask_id=1,
					stock_id=1,
					user_id=2,
					amount=5,
					price=7
				},
	insert_to_DB(Ask1),
	
Ask2=#ask_queue{ask_id=2,
					stock_id=3,
					user_id=3,
					amount=10,
					price=5
				},
	insert_to_DB(Ask2),
%--------------------@@@@@@@@@@@@@@@@@@@@@@@@@@@_____COUNTERS____  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@---------------------	
Counter1=#counters{name= ask_counter ,value= 20},
insert_to_DB(Counter1),
	
	
Counter2=#counters{name= bid_counter ,value= 20},
insert_to_DB(Counter2),
	

Counter3=#counters{name= stockTableCounter ,value= 20},
insert_to_DB(Counter3),

Counter4=#counters{name= company_counter ,value= 20},
insert_to_DB(Counter4),

Counter5=#counters{name= favorite_counter ,value= 20},
insert_to_DB(Counter5).

run()->
	mnesia:stop(),
	mnesia:delete_schema([node()]),
	f(),
	init(),
	example(),
	check_deals:t().

%%
%% Local Functions
%%
